package com.skytech.tax.customsdatawarehouse.singlewindow.taxhandle.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.taxhandle.domain.entity.TaxBillFile;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author Lianhong
 * @since 2021/11/30
 */
@Repository
public class TaxBillFileDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String selectTaxBillFileSql = "SELECT file FROM sw_tax_bill_file WHERE swTaxId = ?";

    public String findBySwTaxId(String swTaxId) {
        return namedParameterJdbcTemplate.getJdbcOperations().queryForObject(selectTaxBillFileSql,
                new Object[]{swTaxId}, String.class);
    }

    private String insertTaxBillFileSql = "INSERT INTO sw_tax_bill_file (swTaxId, file, storageTime) VALUES (:swTaxId, :file, :storageTime)";

    public void save(TaxBillFile taxBillFile) {
        taxBillFile.setStorageTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        this.namedParameterJdbcTemplate.update(this.insertTaxBillFileSql, new BeanPropertySqlParameterSource(taxBillFile));
    }

    private String existsTaxBillFileSql = "SELECT count(1) FROM sw_tax_bill_file WHERE swTaxId = ?";

    public boolean exists(String swTaxId) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsTaxBillFileSql, new Object[]{swTaxId}, Integer.class);
        return count != null && count > 0;    }
}
